namespace Web.Control.WWW.Wiki
{
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using Lx.Web.Common.UI;
    using Lx.Web.Common.ControlsUI;
    using System.Collections;
    using System.Collections.Generic;
    using System.Xml;
    using System.IO;
    using System.Text;

    using BLF;
    using BLF.WWW_WWW.Wiki;
    using Model.WWW_WWW.Wiki;

    public class Print : BaseControl
    {
        protected Literal lblContent;

        PageInfo page = null;
        PageContent content = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            page = Pages.Instance.FindPage(Request["Page"]);
            if (page == null) Response.Redirect("default.htm");

            content = BLF.WWW_WWW.Wiki.Content.GetPageContent(page, true);
            PrintContent();
        }

        public void PrintContent()
        {
            StringBuilder sb = new StringBuilder();
            if (Request["Discuss"] == null)
            {
                sb.Append(@"<h1 class=""pagetitle"">" + content.Title + "</h1>");
                sb.Append(BLF.WWW_WWW.Wiki.Content.GetFormattedPageContent(page, true));
            }
            else
            {
                sb.Append(@"<h1 class=""pagetitle"">" + content.Title + " - Discussion</h1>");
                PrintDiscussion(sb);
            }
            lblContent.Text = sb.ToString();
        }

        private void PrintDiscussion(StringBuilder sb)
        {
            List<Message> messages = new List<Message>(Pages.Instance.GetPageMessages(page));
            if (messages.Count == 0)
            {
                sb.Append("<i>");
                sb.Append(Exchanger.ResourceExchanger.GetResource("NoMessages"));
                sb.Append("</i>");
            }
            else PrintSubtree(messages, null, sb);
        }

        /// <summary>
        /// Prints a subtree of Messages depth-first.
        /// </summary>
        /// <param name="messages">The Messages.</param>
        private void PrintSubtree(List<Message> messages, Message parent, StringBuilder sb)
        {
            for (int i = 0; i < messages.Count; i++)
            {
                sb.Append(@"<div" + (parent != null ? @" class=""messagecontainer""" : @" class=""rootmessagecontainer""") + ">");
                PrintMessage(messages[i], parent, sb);
                PrintSubtree(messages[i].Replies, messages[i], sb);
                sb.Append("</div>");
            }
        }

        private void PrintMessage(Message message, Message parent, StringBuilder sb)
        {
            // Header
            sb.Append(@"<div class=""messageheader"">");
            string shift = "";
            if (Request.Cookies[Settings.CultureCookieName] != null) shift = Request.Cookies[Settings.CultureCookieName]["T"];
            sb.Append(FormatDiffDate(GetText("C0036"), Model.CommonMethod.FormatDateAgo(message.DateTime)));
            sb.Append(" ");
            sb.Append(Exchanger.ResourceExchanger.GetResource("By"));
            sb.Append(" ");
            sb.Append(message.NickName);

            // Subject
            if (message.Subject.Length > 0)
            {
                sb.Append(@"<br /><span class=""messagesubject"">");
                sb.Append(message.Subject);
                sb.Append("</span>");
            }

            sb.Append("</div>");

            // Body
            sb.Append(@"<div class=""messagebody"">");
            sb.Append(FormattingPipeline.FormatWithPhase1And2(message.Body, page));
            sb.Append("</div>");
        }

        
    }
}
